/**
 * 商品标签管理
 */
var queryGoodsLableSearch = function() {
	var queryGoodsLableFieldSet = new Ext.form.FieldSet({
				id : 'queryGoodsLableFieldSet',
				title : '查询条件',
				collapsible : true,// 可折叠
				buttonAlign : 'center',
				keys : [{
							key : 13, // 13代表回车
							fn : queryGoodsLable,
							scope : this
						}],
				items : [{
					layout : 'column',
					border : false,
					items : [{
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											id : "goodsLable_goodsLableName",
											xtype : 'textfield',
											anchor : '100%',
											fieldLabel : '标签名称',
											name : "lableName"
										}]
							},{
							columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											id : 'goodsLable_goodsId',
											xtype : 'goodsCmb',
											fieldLabel : "商品名称",
											allowBlank : true,
											hiddenName : 'goodsId',
											showValue : true,
											existall : true,
											defaultFirstRow : 1,
											statusArr : [0, 1]
										}]
							},{
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
											xtype : 'dataDictionaryCmb',
											anchor : '100%',
											fieldLabel : "标签状态",
											editable : false,
											autoLoad : false,
											existall : true,
											defaultFirstRow : 1,
											hiddenName : 'lableStatus',
											dictionaryType : PmsUtil.lable_status
										}]
							}, {
								columnWidth : .25,
								layout : 'form',
								border : false,
								items : [{
									layout : 'column',
									border : false,
									items : [{
												text : '查询',
												xtype : 'button',
												width : 80,
												handler : function() {
													queryGoodsLable();
												},
												style : {
													marginLeft : "100px",
													marginRight : "10px"
												}
											}, {
												text : '重置',
												xtype : 'button',
												width : 80,
												handler : function() {
													queryGoodsLableForm.getForm()
															.reset();
												}
											}]
								}]
							}]
				}]
			});
	fieldSetListeners(['queryGoodsLableFieldSet', 'queryGoodsLableForm',
					'goodsLableManagerGrid'], [0.12, 0.88]);

	var queryGoodsLableForm = new Ext.form.FormPanel({
				id : 'queryGoodsLableForm',
				anchor : '100% 12%',
				frame : true,
				labelAlign : 'right',
				bodyBorder : false,
				items : [queryGoodsLableFieldSet]
			});
	return queryGoodsLableForm;
};

/**
 * grids
 */

var goodsLableManagerGrid = function() {
	// 定义表的每列（对应表头信息）
	var goodsLableManagerCm = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(), {
				dataIndex : "id",
				header : "标签编号",
				sortable : true,
				width : 70
			}, {
				dataIndex : "lableName",
				header : "标签名称",
				sortable : true,
				width : 90,
				renderer : function(value, meta, record) {
					if (value) {
						meta.attr = 'ext:qtitle="标签名称:" ext:qtip="' + value
								+ '"';
						return value;
					}
				}
			}, {
				dataIndex : "goodsName",
				header : "商品名称",
				sortable : true,
				width : 120
			}, {
				dataIndex : "orderList",
				header : "主题排序",
				sortable : true,
				width : 70
			}, {
				dataIndex : "lableStatus",
				header : "标签状态",
				sortable : true,
				width : 70,
				renderer : function(value) {
					return PmsUtil.lableStatus(value);
				}
			}, {
				dataIndex : "createdDate",
				header : "创建时间",
				sortable : true,
				width : 130
			}, {
				dataIndex : "createdName",
				header : "创建人",
				sortable : true,
				width : 60
			}, {
				dataIndex : "updatedDate",
				header : "修改时间",
				sortable : true,
				width : 130
			}, {
				dataIndex : "updatedName",
				header : "修改人",
				sortable : true,
				width : 60
			}, {
				header : "管理",
				width : 250,
				dataIndex : "id",
				sortable : false,
				renderer : function(j, i, g, m, h) {
					var l = g.data.id;
					var lableStatus = g.data.lableStatus;
					var lableName = g.data.lableName;
					var k = "";
					if ( isGranted("goodsLableManager_修改标签")) {
						k += '&nbsp;&nbsp;<a href="#" onclick="updateGoodsLable('
								+ l + ')\" >标签修改</a>';
					}
					if (isGranted("goodsLableManager_删除商品标签")) {
						k += '&nbsp;&nbsp;<a href="#" onclick="delGoodsLable('
								+ l + ');" >删除</a>';
					}
					return k;
				}
			}]);

	// 定义分析器
	var goodsLableManagerGrid = new Ext.data.JsonReader({
				totalProperty : "totalCount",
				root : "list",
				fields : [{
							name : "id"
						}, {
							name : "lableName"
						}, {
							name : "goodsName"
						}, {
							name : "goodsId"
						}, {
							name : "orderList"
						}, {
							name : "lableStatus"
						}, {
							name : "createdDate"
						}, {
							name : "createdName"
						}, {
							name : "updatedDate"
						}, {
							name : "updatedName"
						}]
			});

	// 取数据store
	var goodsLableViewStore = new Ext.data.Store({
				autoLoad : true,// 要加载数据必须设置为true
				proxy : new Ext.data.HttpProxy({
							url : __ctxPath + "/goodsManager/selectGoodsLable"
						}),
				remoteSort : false,
				baseParams : {
					start : 0,
					limit : 25
				},
				listeners : {
					beforeload : {
						fn : function(thiz, options) {
							Ext.apply(thiz.baseParams, Ext
											.getCmp('queryGoodsLableForm')
											.getForm().getValues());
						}
					}
				},
				reader : goodsLableManagerGrid
			});

	// 定义grid的toolbar
	var goodsLableTb = new Ext.Toolbar({
				id : "goodsLableTb",
				height : 28,
				items : []
			});
	goodsLableTb.add(new Ext.scm.Button({
				text : "标签添加",
				width : 80,
				iconCls : "btn-add",
				pressed : true,
				hsValue : 'goodsLableManager_新建标签',
				listeners : {
					"click" : function() {
						addGoodsLable();
					}
				}
			}));
	goodsLableTb.add(new Ext.Toolbar.Spacer());
	goodsLableTb.add(new Ext.scm.Button({
				text : "刷 新",
				iconCls : "btn-refresh",
				width : 80,
				pressed : true,
				hsValue : 'goodsLableManager_刷新',
				handler : function() {
					queryGoodsLable();
				}
			}));
	
	var goodsLableManagerGrid = new Ext.grid.GridPanel({
				id : 'goodsLableManagerGrid',
				store : goodsLableViewStore,
				cm : goodsLableManagerCm,
				autoScroll : true,
				loadMask : true,
				stripeRows : true,
				columnLines : true,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {
					emptyText : '没有您想要的数据!'
				},
				anchor : '100% 88%',
				frame : true,
				tbar : goodsLableTb,
				bbar : new PG.PagingBar({
							id : 'goodsLableGridPage',
							store : goodsLableViewStore
						})
			});
	return goodsLableManagerGrid;
};

// 查询
function queryGoodsLable() {
	if (Ext.getCmp('queryGoodsLableForm').getForm().isValid()) {
		var grid = Ext.getCmp('goodsLableManagerGrid');
		var store = grid.store;
		var pagesize = Ext.getCmp('goodsLableGridPage').pageSize;
		store.load({
					params : {
						start : 0,
						limit : pagesize
					}
				});
	}
}

/**
 * 添加商品
 * 
 * @param {}
 *            goodsLableId
 */
function addGoodsToGoodsLable(GoodsLableId, GoodsLableName) {
	try {
		addGoodsToSubj(GoodsLableId, GoodsLableName, "添加商品").show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/GoodsLable/addGoodsForm.js'],
				function() {
					addGoodsToSubj(GoodsLableId, GoodsLableName, "添加商品").show();
				});
	}
}

/**
 * 新建主题
 */
function addGoodsLable(goodsId) {
	try {
		addOrUpdGoodsLable(null, "标签添加", goodsId).show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/goods/goodsLableForm.js'], function() {
					addOrUpdGoodsLable(null, "标签添加", goodsId).show();
				});
	}
}
/**
 * 修改标签
 * 
 * @param {}
 *            goodsLableId
 */
function updateGoodsLable(id) {
	try {
		addOrUpdGoodsLable(id, "修改标签").show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/goods/goodsLableForm.js'], function() {
					addOrUpdGoodsLable(id, "修改标签").show();
				});
	}

}
/**
 * 修改标签状态信息
 * 
 * @param {}
 *            GoodsLableId
 * @param {}
 *            status
 * @param {}
 *            message
 */
function updateGoodsLableStatus(GoodsLableId, status, message) {
	Ext.Msg.confirm("信息确认", "是否" + message + "该条主题信息?", function(c) {
		if (c == "yes") {
			var optionMask = new Ext.LoadMask(Ext.getBody(), {
						msg : "正在处理,请稍后..."
					});
			optionMask.show();
			Ext.Ajax.request({
						url : __ctxPath + "/GoodsLableManager/updateGoodsLableStatus",
						params : {
							GoodsLableId : GoodsLableId,
							status : status
						},
						method : "post",
						success : function(response) {
							optionMask.hide();
							var jsonData = Ext.util.JSON
									.decode(response.responseText);
							if (jsonData.success) {
								Ext.MessageBox.show({
											title : "信息提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.INFO
										});
								queryGoodsLable();
							} else {
								Ext.MessageBox.show({
											title : "错误提示",
											msg : jsonData.data,
											buttons : Ext.MessageBox.OK,
											icon : Ext.MessageBox.ERROR
										});
							}
						},
						failure : function(response) {
							optionMask.hide();
							Ext.MessageBox.show({
										title : "操作信息",
										msg : "服务器请求异常,请稍后再试!",
										buttons : Ext.MessageBox.OK,
										icon : Ext.MessageBox.ERROR
									});
						}
					});
		}
	});
};

// 删除
function delGoodsLable(goodsLableId) {
	Ext.Msg.confirm("信息确认", "是否删除该条商品图片信息?", function(btn) {
				if (btn == "yes") {
					var optionMask = new Ext.LoadMask(Ext.getBody(), {
								msg : "正在处理,请稍后..."
							});
					optionMask.show();
					Ext.Ajax.request({
								url : __ctxPath
										+ "/goodsManager/deleteGoodsLableById",
								params : {
									goodsLableId : goodsLableId
								},
								method : "post",
								success : function(response) {
									optionMask.hide();
									var jsonData = Ext.util.JSON
											.decode(response.responseText);
									if (jsonData.success) {
										Ext.MessageBox.show({
													title : "信息提示",
													msg : jsonData.data,
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.INFO
												});
										queryGoodsLable();
									} else {
										Ext.MessageBox.show({
													title : "错误提示",
													msg : jsonData.data,
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.ERROR
												});
									}
								},
								failure : function(response) {
									optionMask.hide();
									Ext.MessageBox.show({
												title : "操作信息",
												msg : "服务器请求异常,请稍后再试!",
												buttons : Ext.MessageBox.OK,
												icon : Ext.MessageBox.ERROR
											});
								}
							});
				}
			});
}


Ext.getCmp("centerTabPanel").getActiveTab().add(queryGoodsLableSearch(),
		goodsLableManagerGrid());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();
